﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class UserNewForm : Form
    {
        #region Fields
        UAC_UserManagement um = new UAC_UserManagement();
        UAC_BasicDetails newbasic;
        UAC_AdvancedDetails newadvanced;
        UAC_MarketingDetails newmarket;
        UAC_Disabilities dissability;
        UAC_EducationalBackground educationalBackground;
        #endregion

        #region Constructor
        public UserNewForm()
        {
            InitializeComponent();
            SetComboBox();
        }
        #endregion

        #region Buttons
        private void BtnRegister_Click(object sender, EventArgs e)
        {
            bool check = true;
            foreach (Control item in gbxbasic.Controls)
            {
                if (item as TextBox != null)
                {
                    if (string.IsNullOrEmpty(item.Text))
                    {
                        check = false;
                    }
                }
            }
            foreach (Control item in gbxSecurity.Controls)
            {
                if (item as TextBox != null)
                {
                    if (string.IsNullOrEmpty(item.Text))
                    {
                        check = false;
                    }
                }
            }
            if (check)
            {
                SetBasic();
                SetAdvanced();
                SetMarket();
                um.Register(txtPassword.Text, txtSecretQ.Text, txtSecretA.Text, newbasic, newadvanced, newmarket, educationalBackground);
                MessageBox.Show(this, "User Registered");
            }
            else
            {
                MessageBox.Show(this, "Please enter all basic and security details");
            }

        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region Create User Mathods
        private void SetBasic()
        {
            newbasic = new UAC_BasicDetails(txtFName.Text, txtSName.Text, (UAC_Gender)cbxGender.SelectedItem, txtID.Text, txtEmail.Text, txtContact.Text, cbxType.SelectedText, cbxOccupation.SelectedText);
        }
        private void SetAdvanced()
        {
            UAC_EthnicGroup ethnic;

            if (cbxGender.SelectedIndex != -1)
            {
                ethnic = (UAC_EthnicGroup)cbxGender.SelectedItem;
            }
            else
            {
                ethnic = UAC_EthnicGroup.Other;
            }
            dissability = UAC_Disabilities.Wheelchair | UAC_Disabilities.Other;
            string ii = dissability.ToString();
            if (clbxDisabilities.SelectedIndex != -1)
            {
                foreach (UAC_Disabilities CheckedItem in clbxDisabilities.CheckedIndices)
                {
                    dissability = dissability | CheckedItem;
                }
            }
            else
            {
                dissability = UAC_Disabilities.Other;
            }
            newadvanced = new UAC_AdvancedDetails(dtpDOB.Value, txtHomeLang.Text, txtNationality.Text, txtHomeAddress.Text, txtPostalAddress.Text, txtCity.Text, txtCountry.Text, txtcode.Text, txtmadAid.Text, txtMedAidNum.Text, ethnic, dissability);
        }
        private void SetMarket()
        {
            newmarket = new UAC_MarketingDetails(cbxMedium.SelectedText, txtComment.Text, dtpsubmission.Value, (UAC_OccupationStatus)cbxOccupation.SelectedValue, txtHishSchool.Text);
        }
        private void SetComboBox()
        {
            cbxGender.DataSource = Enum.GetValues(typeof(UAC_Gender));
            cbxGender.SelectedIndex = 0;
            cbxEthnic.DataSource = Enum.GetValues(typeof(UAC_EthnicGroup));
            clbxDisabilities.DataSource = Enum.GetValues(typeof(UAC_Disabilities));
            cbxMedium.DataSource = Enum.GetValues(typeof(UAC_MarketMedium));
            cbxOccupation.DataSource = Enum.GetValues(typeof(UAC_OccupationStatus));
            cbxType.DataSource = Enum.GetValues(typeof(UAC_UserType));
        }
        #endregion

        #region Add instatutions
        private void btnInstatutionAdd_Click(object sender, EventArgs e)
        {
            educationalBackground = new UAC_EducationalBackground();
            if (!string.IsNullOrEmpty(txtInstatution.Text))
            {
                UAC_InstitutionBackground institution = new UAC_InstitutionBackground(txtInstatution.Text, dtpStart.Value, dtpEnd.Value);
                List<I_UAC_InstitutionSubjectResult> subs = EducationDetailsForm.Show(institution);
                if (subs != null)
                {
                    institution.InstitutionSubjects = subs;
                    educationalBackground.Institution.Add(institution);
                    lbxInsList.Items.Add(institution.InstitutionName);
                }
                else
                {
                    MessageBox.Show(this, "Instatution Not Added");
                }
            }
            else
            {
                MessageBox.Show(this, "Please Enter A Instatution Name");
            }
        }
        #endregion
    }
}
